<template>
  <div class="content">
    <div class="header">
      <h3 class="title">活动列表</h3>
    </div>
    <div class="table">
      <el-table :data="eventList" border style="width: 100%">
        <el-table-column
          align="center"
          type="index"
          label="序号"
          width="60px"
        />

        <el-table-column
          align="center"
          label="活动标题"
          prop="title"
          width="150px"
        />
        <el-table-column
          align="center"
          label="社团名称"
          prop="club_name"
          width="150px"
        />
        <el-table-column
          align="center"
          label="限制人数"
          prop="registration_limit"
          width="100px"
        />
        <el-table-column
          align="center"
          label="参加人数"
          prop="registered_count"
          width="100px"
        />
        <el-table-column align="center" label="开始时间" prop="start_date">
          <template #default="scope">
            {{ formatUTC(scope.row.start_date) }}
          </template>
        </el-table-column>
        <el-table-column align="center" label="结束时间" prop="end_date">
          <template #default="scope">
            {{ formatUTC(scope.row.end_date) }}
          </template>
        </el-table-column>

        <el-table-column
          align="center"
          label="活动封面"
          prop="background_image_url"
          width="150px"
        >
          <template #default="scope">
            <img
              style="width: 50px"
              :src="scope.row.background_image_url"
              alt=""
            />
          </template>
        </el-table-column>

        <el-table-column align="center" label="操作" width="200px">
          <template #default="scope">
            <el-button
              size="small"
              icon="document"
              type="info"
              text
              @click="handleContentBtnClick(scope.row)"
            >
              内容详情
            </el-button>
            <el-button
              v-if="role === 2 || ids().includes(scope.row.club_id)"
              size="small"
              icon="document"
              type="success"
              text
              @click="handleDetailBtnClick(scope.row.id)"
            >
              活动详情
            </el-button>
            <el-button
              v-if="role === 2"
              size="small"
              icon="close"
              type="warning"
              text
              @click="handleEditBtnClick(scope.row.id)"
            >
              退回
            </el-button>
            <el-button
              v-if="role === 2 || ids().includes(scope.row.club_id)"
              size="small"
              icon="Delete"
              type="danger"
              text
              @click="handleDeleteBtnClick(scope.row.id)"
            >
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="pagination">
      <el-pagination
        v-model:current-page="currentPage"
        v-model:page-size="pageSize"
        :page-sizes="[10, 20, 30]"
        layout="total, sizes, prev, pager, next, jumper"
        :total="eventTotalCount"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
    <el-dialog v-model="dialogTableVisible" title="活动" :width="900">
      <div v-html="content"></div>
    </el-dialog>
  </div>
</template>

<script setup lang="ts">
import { storeToRefs } from 'pinia'
import { ref } from 'vue'
import useEventStore from '@/store/main/event/event'
import { formatUTC } from '@/utils/format'
import { ElMessage, ElMessageBox } from 'element-plus'
import { useRouter } from 'vue-router'
import { getRole } from '@/utils/handle-role'
import useClubStore from '@/store/main/club/club'
const emit = defineEmits(['newClick', 'editClick'])

const role = ref(getRole())
const clubStore = useClubStore()
const { clubsList } = storeToRefs(clubStore)
// 1.发起action，请求usersList的数据
const eventStore = useEventStore()
const currentPage = ref(1)
const pageSize = ref(10)
const searchData = ref<any>()

fetchEventListData()
// 2.获取usersList数据,进行展示
const { eventList, eventTotalCount } = storeToRefs(eventStore)
console.log(eventList)

// 3.页码相关的逻辑
function handleSizeChange() {
  fetchEventListData()
}
function handleCurrentChange() {
  fetchEventListData()
}
// 4.定义函数, 用于发送网络请求
function fetchEventListData(formData?: any) {
  // 1.获取offset/size
  const limit = pageSize.value
  const offset = (currentPage.value - 1) * limit
  // 2.发起网络请求
  if (formData) {
    searchData.value = formData
    eventStore.getEventAction({ ...formData, limit, offset })
    return
  }
  eventStore.getEventAction({ limit, offset, status: 'ongoing' })
}

// 查看详细内容
const dialogTableVisible = ref(false)
const content = ref('')
function handleContentBtnClick(row: any) {
  content.value = row.description
  dialogTableVisible.value = true
}

// 判断是否在id
function ids() {
  return clubsList.value.map((item) => {
    return item.id
  })
}

// 5.删除数据
async function handleDeleteBtnClick(id: number) {
  ElMessageBox.confirm(`确认删除这个活动吗`, '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(async () => {
    await eventStore.deleteEventAction(id, searchData.value)
    ElMessage({
      type: 'success',
      message: '删除活动',
      duration: 1000
    })
  })
}

// 活动详情
const router = useRouter()
function handleDetailBtnClick(id: number) {
  router.push({
    path: '/main/event/detail',
    query: {
      id
    }
  })
}
// 退回活动
function handleEditBtnClick(id: number) {
  ElMessageBox.confirm(`确认退回这个活动吗`, '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(async () => {
    await eventStore.updateEventStatusAction(id, 'rejected', searchData.value)
    ElMessage({
      type: 'success',
      message: '退回活动成功',
      duration: 1000
    })
  })
}

defineExpose({
  fetchEventListData
})
</script>

<style lang="less" scoped>
.content {
  margin-top: 20px;
  padding: 20px;
  background-color: #fff;
}

.header {
  display: flex;
  justify-content: space-between;
  align-items: flex-end;
  margin-bottom: 10px;

  .title {
    font-size: 22px;
  }
}

.table {
  :deep(.el-table__cell) {
    padding: 12px 0;
  }

  .el-button {
    margin-left: 0;
    padding: 5px 8px;
  }
}

.pagination {
  display: flex;
  justify-content: flex-end;
  margin-top: 10px;
}
</style>
